﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DiscountSharing.LocalControls
{
    public partial class LimitedTextBox : UserControl
    {
        private int wordLimit = 100;
        private string fullString = String.Empty;

        public string Message
        {
            private set { }
            get
            {
                return MessageInput.Text;
            }
        }

        public LimitedTextBox()
        {
            InitializeComponent();

            Loaded += (o, e) =>
                {
                    WordCount.Text = String.Format("{0} word{1} left", wordLimit - MessageInput.Text.Length, (MessageInput.Text.Length == wordLimit - 1) ? String.Empty : "s");
                };
            MessageInput.TextChanged += (o, e) =>
                {
                    if (MessageInput.Text.Length == wordLimit)
                    {
                        fullString = MessageInput.Text;
                    }
                    if (MessageInput.Text.Length > wordLimit)
                    {
                        MessageInput.Text = fullString;
                        MessageInput.Select(MessageInput.Text.Length, 0);
                    }
                    WordCount.Text = String.Format("{0} word{1} left", wordLimit - MessageInput.Text.Length, (MessageInput.Text.Length == wordLimit - 1) ? String.Empty : "s");
                };
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            MessageInput.Focus();
        }
    }
}
